package id.co.rho.scoring.cid.client.mvc.scoringmodel;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelCommand;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelCommand.CommandType;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelResult;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class ScoringModelEditView extends View{
	private ScoringModelEditUI ui;
	private CommandDispatcherAsync dispatcher;
	private Credential credential;
	private boolean addData = false;
	private long id;
	private long parentId;
	private long modelId;
	@Inject
	public ScoringModelEditView(ScoringModelEditUI ui, Credential credential,
			CommandDispatcherAsync dispatcher) {
		super(null);
		this.ui = ui;
		controller = new ViewController();
		this.dispatcher = dispatcher;
		this.credential = credential;
	}

	@Override
	public void initialize() {
		super.initialize();
		ui.initialize();
		ui.getBtnSave().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						saveData();
					}
				});
		ui.getBtnCancel().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						AppEvent appEvent = new AppEvent(
								AppEvents.ScoringModel);
						Dispatcher.forwardEvent(appEvent);
					}
				});
	}
	
	private void saveData() {
		ScoringModelCommand cmd = new ScoringModelCommand();
		
		if (addData == true) {
			cmd.setCommandType(CommandType.ADD);
		} else {
			cmd.setCommandType(CommandType.EDIT);
			cmd.setId(id);
		}
		
		cmd.setCriteriaNumber(ui.getTxtNumber().getValue());
		cmd.setCriteriaName(ui.getTxtName().getValue());
		cmd.setCriteriaRank(Double.parseDouble(ui.getTxtRank().getValue()));
		cmd.setCriteriaWeight(Double.parseDouble(ui.getTxtWeight().getValue()));
		cmd.setCriteriaType(ui.getCboType().getSimpleValue());
		cmd.setModelId(modelId);
		
		cmd.setParent(parentId);
		
		dispatcher.execute(cmd, new AsyncCallback<ScoringModelResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(ScoringModelResult result) {
				if (result.isSaved() == initialized) {
					Info.display("Saved", "Data has been saved");
					AppEvent appEvent = new AppEvent(
							AppEvents.ScoringModel);
					appEvent.setData("modelId",modelId);
					Dispatcher.forwardEvent(appEvent);
				}
			}


		});
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.ScoringModelEdit)) {
			if (event.getData("id") != null) {
				id = Long.parseLong(event.getData("id").toString());
				addData = false;
				ui.clearText();
				if(event.getData("number") != null) ui.getTxtNumber().setValue(event.getData("number").toString());
				if(event.getData("name") != null) ui.getTxtName().setValue(event.getData("name").toString());
				if(event.getData("rank")!=null) ui.getTxtRank().setValue(event.getData("rank").toString());
				if(event.getData("weight")!=null) ui.getTxtWeight().setValue(event.getData("weight").toString());
				this.modelId = event.getData("modelId");
				if(event.getData("type") != null) ui.getCboType().setSimpleValue(event.getData("type").toString());
				if(event.getData("parent") != null){
					parentId = Long.valueOf(event.getData("parent").toString());
				}else{parentId=(Long) null;}
				ui.show();
			} else {
				parentId = Long.valueOf(event.getData("parent").toString());	
				if(parentId != 0L){
					this.modelId = event.getData("modelId");
					addData = true;
					id = 0;
					ui.clearText();
					ui.show();
				}else{
					this.modelId = event.getData("modelId");
					addData = true;
					id = 0;
					ui.clearText();
					ui.show();
				}
			}
		}
	}
	
	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.ScoringModelEdit);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(ScoringModelEditView.this, event);
		}

	}

}
